#include "KMeansLearn.h"

CKMeansLearn::CKMeansLearn(const shared_ptr<CKmeansNetwork> & pNN) :
	m_NN(pNN)
{}

void CKMeansLearn::Trainning(const Vector & trainData)
{
	int JCode = 0;

	do
	{
		JCode = this->FindNearCenter(trainData);

		this->MoveCenter(trainData, JCode);

	} while (this->FindNearCenter(trainData) != JCode);
}

int CKMeansLearn::FindNearCenter(const Vector & trainingData)
{
	int K = this->m_NN->m_K;

	int minJ = 0;
	double minNorm = this->m_NN->m_KMeans[minJ]->Norm(trainingData);

	for (int i = 1; i < K; i++)
	{
		double normValue = this->m_NN->m_KMeans[i]->Norm(trainingData);

		if (normValue < minNorm)
		{
			minNorm = normValue;
			minJ = i;
		}
	}

	return minJ;
}

void CKMeansLearn::MoveCenter(const Vector & trainingData, int J)
{
	this->m_NN->m_KMeans[J]->InsertMeansPosition(trainingData);
}